package bluejack.game.gal;
import bluejack.game.gal.diaplay.Image;
import bluejack.game.gal.diaplay.ImageButton;
import bluejack.game.gal.diaplay.TextWin;
import bluejack.game.gal.events.SelectEvent;
import com.eclecticdesignstudio.motion.Actuate;
import nme.display.BitmapData;
import nme.display.Sprite;
import nme.events.Event;
import nme.events.MouseEvent;
import nme.Lib;

/**
 * ...
 * @author BlueJack
 */

class GALView extends Sprite
{
	public var control(getControl, null):GALControl;
	private function getControl():GALControl {
		return facde.control;
	}
	private var facde:GALFacde;
	private var textWin:TextWin;
	private var bg:Image;
	private var figureLayer:Sprite;
	private var figures:Array<Image>;
	private var titleMenu:Sprite;

	public function new(facde:GALFacde) {
		super();
		this.facde = facde;
		bg = new Image();
		addChild(bg);
		figureLayer = new Sprite();
		addChild(figureLayer);
		figures = [];
		for (i in 0...3) {
			figures[i] = new Image();
			figures[i].autoCenterX = true;
			figures[i].x = 150 + i * 250;
			figureLayer.addChild(figures[i]);
		}
		textWin = new TextWin();
		textWin.visible = false;
		addChild(textWin);
		titleMenu = new Sprite();
		titleMenu.x = 600;
		titleMenu.y = 200;
		addChild(titleMenu);
		var startBtn:ImageButton = new ImageButton("start");
		startBtn.addEventListener(MouseEvent.CLICK, startNewGame);
		titleMenu.addChild(startBtn);
		var exitBtn:ImageButton = new ImageButton("exit");
		exitBtn.addEventListener(MouseEvent.CLICK, exitGame);
		exitBtn.y = 50;
		titleMenu.addChild(exitBtn); 
		
	}
	
	public function showTitle():Void {
		loadBg("titlePic.jpg");
		titleMenu.visible = true;
	}
	
	public function setDelay(delay:Int = 30):Void {
		textWin.setDelay(delay);
	}
	
	private function exitGame(e:Event):Void {
		Lib.trace("exitGame");
		Lib.exit();
	}
	
	private function startNewGame(e:Event = null):Void {
		Lib.trace("startNewGame");
		titleMenu.visible = false;
		control.run();
	}
	
	public function loadBg(path:String=null,duration:Float=2):Void {
		if (path != null) {
			bg.load("bg/" + path);
			bg.alpha = 0;
			Actuate.tween(bg, duration, {alpha:1 } );
		}else {
			Actuate.tween(bg, duration, { alpha:0 } );
		}
	}
	
	public function loadFigure(path:String, index:Int,fadeIn:Bool=false,flip:Float=1):Void {
		figures[index].load("figure/" + path);
		figures[index].scaleX = flip;
		if (fadeIn) {
			figures[index].alpha = 0;
			Actuate.tween(figures[index], 1, { alpha:1 } );
		}else {
			Actuate.apply(figures[index], { alpha:1 } );
		}
		//figures[index].visible = true;
	}
	
	public function hideFigure(index:Int):Void {
		//figures[index].visible = false;
		Actuate.tween(figures[index], 1, { alpha:0 } );
	}
	
	public function showName(str:String = null,color:Int=0xffffff):Void {
		textWin.showName(str,color);
	}
	
	//private function onTextComplete(e:Event):Void {
		//textWin.removeEventListener(TextWin.SHOW_TEXT_COMPLETE, onTextComplete);
		//textWin.visible = false;
		//control.isWait = false;
		//control.run();
	//}
	
	public function setTextWinStyle(position:Int = 2, showSkin:Bool = true):Void {
		textWin.setStyle(position, showSkin);
	}
	
	public function showText(str:String):Void {
		control.isWait = true;
		textWin.visible = true;
		textWin.showText(str);
		stage.addEventListener(MouseEvent.CLICK, onClick);
	}
	
	private function onClick(e:MouseEvent):Void {
		if (textWin.isComplete) {
			stage.removeEventListener(MouseEvent.CLICK, onClick);
			textWin.visible = false;
			control.isWait = false;
			control.run();
		}else {
			textWin.onClick();
		}
	}
	
	public function showSelect(strArr:Array<String>):Void {
		control.isWait = true;
		control.selectIndex = 5;
		textWin.visible = true;
		textWin.showSelect(strArr);
		textWin.addEventListener(SelectEvent.SELECT, onSelect);
	}
	
	private function onSelect(e:SelectEvent):Void {
		textWin.removeEventListener(SelectEvent.SELECT, onSelect);
		textWin.visible = false;
		control.selectIndex = e.index;
		Lib.trace(control.selectIndex);
		control.isWait = false;
		control.run();
	}
}